﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using DevExpress.XtraEditors.Controls;
using Entities;

namespace QLNhaSach.Modules
{
    public partial class ThemSach : UserControl
    {
        private class Item
        {
            public string Name;
            public int Value;
            public Item( int value, string name)
            {
                Name = name; Value = value;
            }
            public override string ToString()
            {
                // Generates the text shown in the combo box
                return Name;
            }
        }
        DataTable table = new DataTable();
        
        void repositoryItemComboBox_ParseEditValue(object sender, ConvertEditValueEventArgs e)
        {
            e.Value = e.Value.ToString();
            e.Handled = true;
        }
        public ThemSach()
        {
            InitializeComponent();
            table.Columns.Add("STT");
            table.Columns.Add("Tên sách");
            table.Columns.Add("Thể loại");
            table.Columns.Add("Tác giả");
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboTL = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboTL.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            comboTL.ParseEditValue += new ConvertEditValueEventHandler(repositoryItemComboBox_ParseEditValue);
            comboTL.Items.Clear();
            DevExpress.XtraEditors.Repository.RepositoryItemComboBox comboTG = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            comboTG.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            comboTG.ParseEditValue += new ConvertEditValueEventHandler(repositoryItemComboBox_ParseEditValue);
            comboTG.Items.Clear();
            TheLoaiBO tlbo = new TheLoaiBO();
            DataTable tl = tlbo.GetDanhSachTheLoai();
            TacGiaBO tgbo = new TacGiaBO();
            DataTable tg = tgbo.GetDanhSachTacGia();
            foreach (DataRow dt in tl.Rows)
            {
                comboTL.Items.Add(new Item(int.Parse(dt[0].ToString()), dt[1].ToString()));
            }
            foreach (DataRow dt in tg.Rows)
            {
                comboTG.Items.Add(new Item(int.Parse(dt[0].ToString()), dt[1].ToString()));
            }
            for (int i = 0; i < 15; i++)
            {
                table.Rows.Add(i + 1, "", "", "");
            }
            
            gridControl1.DataSource = table;
            gridView1.Columns["Thể loại"].ColumnEdit = comboTL;
            gridView1.Columns["Tác giả"].ColumnEdit = comboTG;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0)
            {
                int soLuong = table.Rows.Count;
                table.Rows.Add(soLuong + 1, "", "", "", "");
            }
            else
            {
                table.Rows.Add("1", "", "", "", "");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            int soLuong = table.Rows.Count;
            if (gridView1.RowCount > 0)
            {
                table.Rows.RemoveAt(soLuong - 1);
            }
        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            int flag = 0;
            try
            {
                for (int count = 0; count < table.Rows.Count; count++)
                {
                    string tenSach = gridView1.GetRowCellValue(count, "Tên sách").ToString();
                    string theLoai = gridView1.GetRowCellValue(count, "Thể loại").ToString();
                    string tacGia = gridView1.GetRowCellValue(count, "Tác giả").ToString();
                    if (tenSach != "" || theLoai != "" || tacGia != "")
                    {
                         if (tenSach == "")
                        {
                            MessageBox.Show("Tên sách không được rỗng.");
                            return;

                        }
                        else if (theLoai == "")
                        {
                            MessageBox.Show("Bạn chưa chọn thể loại");
                            return;

                        }
                        else if (tacGia == "")
                        {
                            MessageBox.Show("Bạn chưa chọn tác giả");
                            return;

                        }
                        else 
                        {

                            Sach s = new Sach();
                            s.TenSach = tenSach;
                            TacGia tg = new TacGia();
                            TacGiaBO tgbo = new TacGiaBO();
                            tg = tgbo.GetIDByName(tacGia);
                            TheLoai tl = new TheLoai();
                            TheLoaiBO tlbo = new TheLoaiBO();
                            tl = tlbo.GetIDByName(theLoai);
                            s.MaTheLoai = tl.MaTheLoai;
                            s.MaTacGia = tg.MaTacGia;
                            s.SoLuongTon = 0;
                            s.DonGia = 0;
                            SachBO sbo = new SachBO();
                            sbo.InsertSach(s);
                            flag = 1;
                        }
                    }
                   
                }
                if (flag == 1)
                {
                    MessageBox.Show("Thêm sách thành công");
                }

            }
            catch
            {
                MessageBox.Show("Thêm sách thất bại");
            }
           
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            table.Clear();
            for (int i = 0; i < 15; i++)
            {
                table.Rows.Add(i + 1, "", "", "");
            }

            gridControl1.DataSource = table;
        }
    }
}
